package com.yh.hljapp.modules.adatper;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yh.hljapp.R;
import com.yh.hljapp.gddb.gbmanager.AddSale_Manager;
import com.yh.hljapp.gddb.gbmanager.BackMatManager;
import com.yh.hljapp.gddb.gbmanager.MatCountManager;
import com.yh.hljapp.gddb.gdbean.AddSaleOut_HeadBean;
import com.yh.hljapp.modules.recycleviewutil.onMoveAndSwipedListener;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

import static android.R.attr.type;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：BackMatListAdapter
 * <p>
 * 创建时间：2017.06.04.10:07
 * *****************************
 */
public class AddSale_out_ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements onMoveAndSwipedListener {


    private Context ctx;
    private List<AddSaleOut_HeadBean> datas;

    public AddSale_out_ListAdapter(Context ctx, List<AddSaleOut_HeadBean> datas) {
        this.ctx = ctx;
        this.datas = datas;
    }

    public void setDatas(List<AddSaleOut_HeadBean> datas) {
        this.datas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(ctx).inflate(R.layout.item_mat_wl, parent,
                false);
        return new MatViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final AddSaleOut_HeadBean head = datas.get(position);
        ((MatViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onItemClick(head);
                }
            }
        });
        ((MatViewHolder) holder).tvFhck.setText(head.getFDCStockIDName());
        ((MatViewHolder) holder).tvGhdw.setText(head.getFSupplyIDName());
        ((MatViewHolder) holder).tvXsfs.setText(head.getFPUserName());
        ((MatViewHolder) holder).tvYwy.setText(head.getFYUserName());

        ((MatViewHolder) holder).tvGhUnit.setText("购货单位：");
        ((MatViewHolder) holder).tvItemCkNum1.setText("业务员：");
        ((MatViewHolder) holder).tvFhWarehouse.setText("发货仓库：");
        ((MatViewHolder) holder).tvItemCkNum2.setText("发货：");
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    private OnItemClickListener listener;

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        return false;
    }

    @Override
    public void onItemDismiss(int position) {

        Long id = datas.get(position).getId();
        AddSale_Manager.getInstance(ctx).delAllByIdOut(id);
        AddSale_Manager.getInstance(ctx).delAllByHeadIdOut(id);//提交成功后删除缓存
        //删除mItems数据
        datas.remove(position);
        //删除RecyclerView列表对应item
        notifyItemRemoved(position);
    }

    public interface OnItemClickListener {
        void onItemClick(AddSaleOut_HeadBean head);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    static class MatViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.tv_jlbh)
        TextView tvJlbh;
        @InjectView(R.id.tv_gh_unit)
        TextView tvGhUnit;
        @InjectView(R.id.tv_ghdw)
        TextView tvGhdw;
        @InjectView(R.id.tv_bmmc1)
        TextView tvBmmc1;
        @InjectView(R.id.tv_item_ck_num1)
        TextView tvItemCkNum1;
        @InjectView(R.id.tv_xsfs)
        TextView tvXsfs;
        @InjectView(R.id.tv_item_ck_num2)
        TextView tvItemCkNum2;
        @InjectView(R.id.tv_ywy)
        TextView tvYwy;
        @InjectView(R.id.tv_fh_warehouse)
        TextView tvFhWarehouse;
        @InjectView(R.id.tv_fhck)
        TextView tvFhck;
        @InjectView(R.id.iv_right)
        ImageView ivRight;
        @InjectView(R.id.ll)
        LinearLayout ll;

        MatViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }
}
